package tools

import (
	"io"
	"log"
	"os"
	"time"

	"github.com/kataras/golog"
	"github.com/kataras/iris/v12/middleware/accesslog"
	"github.com/sirupsen/logrus"
)

func NewLogger() *logrus.Logger {
	const (
		// deleteFileOnExit = false
		// reqLogDir   = dir + "/request/"
		otherLogDir = "logs/other/"
	)

	file := func(filename string) *os.File {
		var (
			file *os.File
			err  error
		)
		if err = os.MkdirAll(otherLogDir, os.ModePerm); err != nil {
			log.Fatal(err)
		}

		file, err = os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
		if err != nil {
			golog.Fatalf("init golog err:%v\n", err)
		}
		return file
	}(otherLogDir + "other_" + time.Now().Format(string(Times_YYYY_MM_DD)) + ".log")
	// GConfig.Boot.LogLevel
	logrus.SetLevel(logrus.WarnLevel)
	logrus.SetFormatter(&logrus.TextFormatter{TimestampFormat: string(Times)})
	logrus.SetOutput(io.MultiWriter(file, os.Stdout))
	return logrus.StandardLogger()
}

func NewRequestLogger() *accesslog.AccessLog {
	reqLogDir := "logs/other/"
	ac := accesslog.File(reqLogDir + "access_" + time.Now().Format(string(Times_YYYY_MM_DD)) + ".log")
	defer ac.Close()
	ac.ResponseBody = false
	ac.LatencyRound = time.Second
	ac.TimeFormat = ""
	//ac.SetFormatter(&accesslog.CSV{Header: true,})
	ac.SetFormatter(&accesslog.JSON{Indent: "  ", HumanTime: true})
	return ac
}
